<!-- 概率评估-查看表格 -->
<template>
  <el-table :data="tableData" :span-method="objectSpanMethod" :header-cell-style="handerMethod" class="normal-table" v-loading="loading" v-if="isShow">
    <el-table-column label="评估区域" prop="" align="center">
      <el-table-column label="" prop="provinceName" align="center">
        <template #default="{ row }">{{ areaName(row.provinceName, row.cityName, row.countyName) }}</template>
      </el-table-column>
      <el-table-column label="" prop="cityName" align="center" />
      <el-table-column label="" prop="countyName" align="center" />
    </el-table-column>
    <!-- 地理id(虚拟ID) 或 房屋单体数据ID -->
    <el-table-column prop="geographyId" align="center" min-width="120px" v-if="isShow">
      <template #header>
        <div>地理ID</div>
        <div>（虚拟的ID）</div>
      </template>
    </el-table-column>
    <template v-if="URL.casualtiesPE == props.url">
      <el-table-column prop="casualties" align="center" min-width="125px">
        <template #header>
          <div>人员伤亡</div>
          <div>（重伤、伤亡）</div>
        </template>
        <template #default="{ row }">
          <DictTag :code="CODE.rysw" :value="row.casualties"></DictTag>
        </template>
      </el-table-column>
      <el-table-column prop="time" align="center" min-width="125px">
        <template #header>
          <div>时间段</div>
          <div>（白天、夜间）</div>
        </template>
        <template #default="{ row }">
          <DictTag :code="CODE.sjd" :value="row.time"></DictTag>
        </template>
      </el-table-column>
    </template>
    <!-- 概率评估-结构破坏 -->
    <template v-if="URL.resistEarthquakesPE == props.url">
      <el-table-column label="破坏程度" prop="damageDegree" align="center" min-width="85px">
        <template #default="{ row }">
          <DictTag :code="CODE.phcd" :value="row.damageDegree"></DictTag>
        </template>
      </el-table-column>
      <el-table-column label="概率值P(t)计算值" prop="pt" align="center" min-width="120px">
        <template #default="{ row }">
          <span v-if="row.pt!=9999.9999">{{ row.pt }}</span>
          <span v-else></span>
        </template>
      </el-table-column>
      <el-table-column label="概率值P(t)修正值" prop="correctionValuePt" align="center" min-width="120px">
        <template #default="{ row }">
          <el-input v-inputNumber="6" v-if="row.pt==9999.9999" :disabled="row.pt==9999.9999"></el-input> 
          <el-input v-model="row.correctionValuePt" v-inputNumber="6" v-else></el-input>
        </template>
      </el-table-column>
    </template>
    <!-- 概率评估-人员伤亡、经济损失 -->
    <template v-if="URL.casualtiesPE == props.url || URL.EconomicLossPD == props.url">
      <el-table-column label="破坏程度" prop="extreme" align="center" min-width="85px" v-if="URL.casualtiesPE == props.url"></el-table-column>
      <el-table-column label="破坏程度" prop="extreme" align="center" min-width="85px" v-if="URL.EconomicLossPD == props.url">
        <template #default="{ row }">
          <DictTag :code="CODE.phcd" :value="row.extreme"></DictTag>
        </template>
      </el-table-column>
      <el-table-column prop="calculatedValue" align="center" v-if="!isShow">
        <template #header>
          <div>统计值</div>
          <span v-if="URL.casualtiesPE == props.url">（人）</span>
          <span v-if="URL.EconomicLossPD == props.url">（万元）</span>
        </template>
        <template #default="{row}">
          <div v-if="URL.casualtiesPE == props.url">{{ row.calculatedValue }}</div>
        </template>
      </el-table-column>
      <el-table-column prop="calculatedValue" align="center" v-if="isShow">
        <template #header>
          <div>期望值</div>
          <span v-if="URL.casualtiesPE == props.url">（人）</span>
          <span v-if="URL.EconomicLossPD == props.url">（万元）</span>
        </template>
        <template #default="{row}">
          <div v-if="URL.casualtiesPE == props.url">{{ row.calculatedValue }}</div>
        </template>
      </el-table-column>
      <el-table-column prop="correctionValue" align="center" v-if="isShow">
        <template #header>
          <div>修正期望值</div>
          <span v-if="URL.casualtiesPE == props.url">（人）</span>
          <span v-if="URL.EconomicLossPD == props.url">（万元）</span>
        </template>
        <template #default="{ row }">
          <el-input v-model="row.correctionValue" v-inputNumber="6"></el-input>
        </template>
      </el-table-column>
      <el-table-column prop="expectValue" align="center" v-if="isShow">
        <template #header>
          <div>期望值</div>
          <span v-if="URL.casualtiesPE == props.url">（人）</span>
          <span v-if="URL.EconomicLossPD == props.url">（万元）</span>
        </template>
        <template #default="{row}">
          <div>{{ row.expectValueTotal }}</div>
        </template>
      </el-table-column>
    </template>
    <el-table-column label="操作" align="center" width="80px">
      <template #default="{ row }">
        <el-button type="text" @click="updateRow(row)" :disabled="row.pt==9999.9999">修改</el-button>
      </template>
    </el-table-column>
  </el-table>
  <el-table :data="tableData" border class="normal-table" v-loading="loading" row-key="id" :default-expand-all="false" v-else>
    <el-table-column label="评估区域" prop="districtName" align="center"/>
    <template v-if="URL.casualtiesPE == props.url">
      <el-table-column prop="casualties" align="center" min-width="125px">
        <template #header>
          <div>人员伤亡</div>
        </template>
        <template #default="{ row }">
          <DictTag :code="CODE.rysw" :value="row.casualties"></DictTag>
        </template>
      </el-table-column>
      <el-table-column prop="time" align="center" min-width="125px">
        <template #header>
          <div>时间段</div>
        </template>
        <template #default="{ row }">
          <DictTag :code="CODE.sjd" :value="row.time"></DictTag>
        </template>
      </el-table-column>
    </template>
    <!-- 概率评估-结构破坏 -->
    <template v-if="URL.resistEarthquakesPE == props.url">
      <el-table-column label="破坏程度" prop="damageDegree" align="center" min-width="85px">
        <template #default="{ row }">
          <DictTag :code="CODE.phcd" :value="row.damageDegree"></DictTag>
        </template>
      </el-table-column>
    </template>
    <!-- 概率评估-人员伤亡、经济损失 -->
    <template v-if="URL.casualtiesPE == props.url || URL.EconomicLossPD == props.url">
      <el-table-column label="破坏程度" prop="extreme" align="center" min-width="85px" v-if="URL.casualtiesPE == props.url"></el-table-column>
      <el-table-column label="破坏程度" prop="extreme" align="center" min-width="85px" v-if="URL.EconomicLossPD == props.url">
        <template #default="{ row }">
          <DictTag :code="CODE.phcd" :value="row.extreme"></DictTag>
        </template>
      </el-table-column>
      <el-table-column prop="value" align="center" v-if="!isShow">
        <template #header>
          <div>统计值</div>
          <span v-if="URL.casualtiesPE == props.url">（人）</span>
          <span v-if="URL.EconomicLossPD == props.url">（万元）</span>
        </template>
      </el-table-column>
    </template>
  </el-table>
</template>
<script setup>
import { URL, CODE } from '@/views/building/index'
import { updateCorrectionValue } from '@/api/building/index'
import { dataMethod } from '@/utils/spanTable.js'
import { areaName } from '@/views/building/index'
import { add } from "@/utils/decimal.js"

const props = defineProps({
  dataList: Array,
  url: String,
  form: Object,
  loading: Boolean,
  isShow:Boolean
})
const emits = defineEmits(['viewTable'])
const { proxy } = getCurrentInstance()

// 合并列
const spanObj = ref()
function objectSpanMethod({ column, rowIndex, columnIndex }) {
  let _row = spanObj.value[column.property] ? spanObj.value[column.property][rowIndex] : 1
  let _col = _row > 0 ? 1 : 0
  if (columnIndex == 0) {
    _col = 3
  }
  if (columnIndex == 1 || columnIndex == 2) {
    _col = 0
  }
  return {
    rowspan: _row,
    colspan: _col
  }
}
// 合并表头
const handerMethod = ({ row, column, rowIndex, columnIndex }) => {
  if (rowIndex == 1) {
    return { display: 'none' }
  }
}

// 修改
const updateRow = row => {
  updateCorrectionValue(props.url, row).then(() => {
    proxy.$modal.msgSuccess('修改成功！')
    emits('viewTable')
  })
}

let tableData = ref([])
watch(() => props.dataList, () => {
  spanObj.value = dataMethod(props.dataList, ['provinceName', 'cityName', 'countyName', 'geographyId'])
  spanObj.value.casualties = spanObj.value.geographyId
  spanObj.value.time = spanObj.value.geographyId
  spanObj.value.expectValue = spanObj.value.geographyId
  tableData.value = props.dataList.map(item => {
    item.expectValueTotal = props.dataList.filter(i => i.geographyId == item.geographyId).reduce((total, cur) => {
      return add(total, cur.correctionValue)
    }, 0)
    return item
  })
}, { deep: true })
</script>
